package com.hns.can.safety.web;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.safety.entity.CommMftCopaEntity;
import com.hns.can.safety.service.FaultTypeService;
import com.hns.can.safety.vo.FaultTypeVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.PubUtil;

/**
 * 故障设置
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */

@Controller
@RequestMapping("/faultType")
public class FaultTypeController extends BaseController{
    
    @Autowired
    FaultTypeService faultTypeService;
    /**
     * 导出文件表头
     */
    private final String[] tableHeaderNames={"故障编码","故障区分代码","故障名称","ECU故障编码","ECU故障名称","故障分类代码","故障等级编码","故障等级名称"};
    /**
     * 导出文件字段名
     */
    private final String[] tableHeaderFields={"mftTypeCode","mftDiscCode","mftTypeName","ecuMftCode","ecuMftName","mftClCode","mftGradeCode","mftGradeName"};
    
    
    /**
     * 查询故障类型
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/searchFaultTypePage")
    @ResponseBody
    public Object searchFaultTypePage(FaultTypeVo vo) throws Exception {
        Pagination page =  faultTypeService.searchFaultTypePage(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 新增
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/addFaultType")
    @ResponseBody
    public Object addFaultType(CommMftCopaEntity entity) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setCrtUser(user.getAcctName());
        entity.setUpdateUser(user.getAcctName());
        entity.setCrtTime(new Date());
        entity.setUpdateTime(new Date());
        int countCode= faultTypeService.getMftCopaByCode(entity);
        if(countCode>0){
            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的故障编码已存在");
        }
        int count= faultTypeService.getMftCopaByName(entity);
        if(count > 0){
            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的故障名称已存在");
        }else{
            faultTypeService.addFaultType(entity);
            return this.getReturnResult("新增成功");
        }
    }
    
    /**
     * 修改
     * @param entity
     * @param response
     * @param request
     * @throws BusinessException
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/editFaultType")
    @ResponseBody
    public Object editFaultType(CommMftCopaEntity entity) throws BusinessException {
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setUpdateUser(user.getAcctName());
        entity.setUpdateTime(new Date());
        int count= faultTypeService.getMftCopaByCodeAndName(entity);
        //根据用户corp新增或修改自己机构的故障类型对照
        if(count>0){
            faultTypeService.editeFaultType(entity);
        }else{
            faultTypeService.addFaultType(entity);
        }
        return this.getReturnResult("保存成功");
    }
  
    
    /**
     * 根据ID获取故障信息
     * @param mftTypeCode
     * @param response
     * @param request
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getFaultTypeById")
    @ResponseBody
    public Object getFaultTypeById(@RequestParam("rowidstr") String rowidstr) throws Exception{
        List<CommMftCopaEntity> entity =faultTypeService.getFaultTypeById(rowidstr);
        return this.getReturnResult(entity);
    }
   
    /**
     * 
     * @param mftTypeCode
     * @param strtusDate
     * @param request
     * @param response
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-7-3
     * @功能说明：删除 
     */
    @RequestMapping("/delFaultType")
    @ResponseBody
    public Object delFaultType(CommMftCopaEntity entity){
        faultTypeService.delFaultType(entity);
        return this.getReturnResult("删除成功");
    }
    
   /**
     * 
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-10-29
     * @功能说明：导出Excel
     */
    @RequestMapping("/exportFaultTypeExcel")
    public void exportFaultTypeExcel(FaultTypeVo vo,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BaseEntity> list = faultTypeService.exportFaultType(vo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "faultType", "故障类型");
    }
    
    /**
     * 
     * @param entity
     * @param request
     * @param response
     * @throws Exception
     * @author:Lan
     * @email:763836737@qq.com
     * @创建日期:2014-10-30
     * @功能说明：导出Pdf
     */
    @RequestMapping("/exportFaultTypePdf")
    public void exportFaultTypePdf(FaultTypeVo vo,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BaseEntity> list = faultTypeService.exportFaultType(vo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", "faultType", "故障类型");
    }
    
}